package com.hetao.xx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.hetao.xx.R;
import com.hetao.xx.orm.NpcEventVo;

import java.util.ArrayList;
import java.util.List;

public class NpcEventAdapter extends BaseAdapter {

    private List<NpcEventVo> list;
    private Context context;
    private View.OnClickListener onClickListener;

    public NpcEventAdapter(Context context, ArrayList<NpcEventVo> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_place_item, null);
            holder = new ViewHolder();
            holder.button = convertView.findViewById(R.id.place_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final NpcEventVo eventVo = list.get(position);
        String name="";
        switch (eventVo.eventType) {
            case GiveTo:
                name = "赠与";
                break;
            case Fight:
                name = "战斗";
                break;
            case SaveArchive:
                name = "存档";
                break;
            case Talk:
                name = "交谈";
                break;
            default:
                name = "未知";
                break;
        }
        holder.button.setText(name);
        holder.button.setTag(eventVo);
        if(onClickListener != null) {
            holder.button.setOnClickListener(onClickListener);
        }
        return convertView;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setList(List<NpcEventVo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class ViewHolder {
        public Button button;

    }
}
